﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebNetCorePhoneErp.Model;
using WebNetCorePhoneErp.IDAL;


namespace WebNetCorePhoneErp.DAL
{
    /// <summary>
    /// 操作日志
    /// </summary>	
    public class Operate_logDALImpl : DaoBase<Operate_log>, IOperate_logDAL
    {
        /// <summary>
        /// 新增操作日志
        /// </summary>
        /// <param name="operate_Log"></param>
        /// <remarks>
        /// 创建时间：2021-6-15 11:28:22 
        /// 作者：王浩力
        /// </remarks>
        public void AddOperateLogAsync(Operate_log operate_Log)
        {
            if (operate_Log == null)
            {
                WebNetCorePhoneErp.Model.Tool.LogHelpter.AddLog("operate_Log不可为空");
                return;
            }
            if (operate_Log.Operate_type <= 0)
            {
                WebNetCorePhoneErp.Model.Tool.LogHelpter.AddLog("Operate_type不可为0，必须大于0");
                return;
            }
            if (string.IsNullOrWhiteSpace(operate_Log.Operator_name))
            {
                WebNetCorePhoneErp.Model.Tool.LogHelpter.AddLog("Operator_name不可为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(operate_Log.Operator_id))
            {
                WebNetCorePhoneErp.Model.Tool.LogHelpter.AddLog("Operator_id不可为空");
                return;
            }
            Task.Run(() =>
            {
                MyDbContext myDbContext = null;
                try
                {
                    operate_Log.Operate_id = Guid.NewGuid().ToString("n");
                    operate_Log.Operate_time = DateTime.Now;

                    myDbContext = new MyDbContext();
                    myDbContext.Add(operate_Log);
                    myDbContext.SaveChanges();
                }
                catch (Exception ex)
                {
                    WebNetCorePhoneErp.Model.Tool.LogHelpter.AddLog("操作日志新增失败:" + ex.ToString());
                }
                finally
                {
                    myDbContext.Dispose();
                }
            });
        }
    }
}